<template>
  <div class="app-container">

    <div class="filter-container">
      <el-input v-model="listQuery.filter.username" placeholder="请输入昵称" style="width: 200px;" col="6"></el-input>
      <el-input v-model="listQuery.filter.phone" placeholder="请输入手机号码" style="width: 200px;" col="6"></el-input>

      <el-select v-model="listQuery.filter.area_id" clearable placeholder="区域">
        <el-option
          v-for="item in formSelections.regions"
          :key="item.id"
          :label="item.title"
          :value="item.id">
        </el-option>
      </el-select>

      <el-select v-model="listQuery.filter.status" clearable placeholder="状态">
        <el-option
          v-for="item in formSelections.statusList"
          :key="item.id"
          :label="item.title"
          :value="item.id">
        </el-option>
      </el-select>
      <el-button type="primary" @click="searchData">查询</el-button>
    </div>

    <el-table :data="list" border style="width: 100%" v-loading="listLoading" fit highlight-current-row>
      <el-table-column fixed prop="id" label="id" width="100"></el-table-column>
      <el-table-column prop="username" label=" 用户名" min-width="120"></el-table-column>
      <el-table-column prop="phone" label=" 手机号码" min-width="120"></el-table-column>
      <el-table-column prop="house_name" label=" 房屋名称" min-width="120"></el-table-column>
      <el-table-column prop="status" label=" 地区" min-width="120">
        <template slot-scope="scope">
          {{ regionFormat(scope.row.area_id)}}
        </template>
      </el-table-column>
      <el-table-column prop="status" label=" 状态" min-width="120">
        <template slot-scope="scope">
          {{ statusFormat(scope.row.status)}}
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="100">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="openDialog(scope.row)">编辑</el-button>
          <el-button type="text" size="small" @click="destroy(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination :total="total" :page="page" :limit="listQuery.limit" :pageSizes="pageSizes"
                @pagination="pageChange"></pagination>

    <el-dialog :title="dialogTitle" :visible.sync="formDialogIsVisible">
      <el-form :model="formData" label-width="80px">

        <el-form-item label="用户名">
          <el-input v-model="formData.username" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="手机号码">
          <el-input v-model="formData.phone" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="房屋名称">
          <el-input v-model="formData.house_name" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="地区">
          <el-select v-model="formData.area_id" placeholder="请选择状态">
            <el-option
              v-for="item in formSelections.regions"
              :key="item.id"
              :label="item.title"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="状态">
          <el-select v-model="formData.status" placeholder="请选择状态">
            <el-option
              v-for="item in formSelections.statusList"
              :key="item.id"
              :label="item.title"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="描述">
          <el-input type="textarea" v-model="formData.remark" autocomplete="off" rows="10"
                    placeholder="请输入描述内容"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="formDialogIsVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import {fetchList, update, destroy, getStatusList} from '@/api/entrust'
  import * as region from '@/api/region'
  import Pagination from '@/components/Pagination' // secondary package based on el-pagination

  export default {
    name: 'entrust',
    components: {Pagination},
    data() {
      return {
        list: null,
        total: 0,
        page: 1,
        pageSizes: [
          10, 20, 30
        ],
        listLoading: true,
        listQuery: {
          offset: 0,
          limit: 10,
          filter: {
            username: null,
            phone: null,
          },
          options: {
            username: "like",
            phone: "like"
          }
        },
        formDialogIsVisible: false,
        formMode: "edit",
        formSelections: {
          statusList: [],
          regions: []
        },
        formData: {
          id: undefined,
          phone: undefined,
          username: undefined,
          house_name: undefined,
          area_id: undefined,
          remark:undefined,
          status: undefined
        }
      }
    },
    computed: {
      dialogTitle() {
        return this.formMode === "add" ? "新增" : "编辑"
      },
    },
    created() {
      this.getList();
      this.init();
    },
    methods: {
      regionFormat(type) {
        let text = "未知";
        this.formSelections.regions.forEach(item => {
          if (parseInt(item.id) === type) text = item.title;
        });
        return text;
      },
      statusFormat(type) {
        let text = "未知";
        this.formSelections.statusList.forEach(item => {
          if (parseInt(item.id) === type) text = item.title;
        });
        return text;
      },
      async init() {
        let statusList = await getStatusList();
        let regionList = await region.fetchList(null);

        this.formSelections.statusList = statusList.data;
        this.formSelections.regions = regionList.data.list;
      },
      pageChange(data) {
        this.page = data.page;
        this.listQuery.limit = data.limit;
        this.listQuery.offset = (this.page - 1) * this.listQuery.limit;
        this.refresh(false);
      },
      openDialog(row) {
        this.formMode = "edit";
        this.formData.id = row.id;
        this.formData.area_id = row.area_id;
        this.formData.remark = row.remark;
        this.formData.username = row.username;
        this.formData.phone = row.phone;
        this.formData.house_name = row.house_name;
        this.formData.status = row.status;
        this.formDialogIsVisible = true;
      },
      save() {
        let promise;
        this.listLoading = true;
        promise = update(this.formData);
        promise.then(() => {
          this.refresh();
        }).catch(() => {
          this.listLoading = false;
        });
      },
      destroy(id) {
        this.$confirm('此操作将永久删除数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          this.listLoading = true;
          await destroy({id});
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch((error) => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        }).finally(() => {
          this.refresh();
        });
      },
      searchData() {
        this.refresh(true);
      },
      refresh(isAll = false) {
        this.formDialogIsVisible = false;
        this.listLoading = false;
        if (isAll) {
          this.page = 1;
        }
        this.getList();
      },
      getList() {
        this.listLoading = true
        fetchList(this.listQuery).then(response => {
          let {data} = response;
          this.list = data.list
          this.total = response.data.count
          this.listLoading = false
        }).catch(() => {
          this.listLoading = false
        });
      },
    },
    watch: {
      page: function (newVluae, oldVluae) {
        this.listQuery.offset = (newVluae - 1) * this.listQuery.limit;
      },
    },
  }
</script>
